<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>Aria: socketClientExample.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Aria
   &#160;<span id="projectnumber">2.9.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('socketClientExample_8cpp-example.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">socketClientExample.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p><a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> example client programThis is an example to show how to use <a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> to create a client and a server. This is the client program. The server is socketServerExample.cpp.</p>
<p>This connects to a server on the host 'localhost' on port 7777. It waits for the server to say hello, then it says hello in reponse. It then closes the socket and exits.</p>
<p>First run the server program socketServerExample. Then run this client program on the same computer, perhaps in a different terminal window. You should see the server accept the connection, send its greeting to this client program, and see this client respond with its greeting text to the server.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classArSocket.html" title="socket communication wrapper ">ArSocket</a> </dd>
<dd>
socketServerExample.cpp</dd></dl>
<p>Also see the ArNetworking library for a more complete networking framework.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;Aria.h&quot;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">  <span class="comment">// The string to send to the server. </span></div><div class="line">  <span class="keyword">const</span> <span class="keywordtype">char</span> *strToSend=<span class="stringliteral">&quot;Hello Server&quot;</span>;</div><div class="line">  <span class="comment">// The buffer in which to recieve the hello from the server</span></div><div class="line">  <span class="keywordtype">char</span> buff[100];</div><div class="line">  <span class="comment">// The size of the string the server sent</span></div><div class="line">  <span class="keywordtype">size_t</span> strSize;</div><div class="line"></div><div class="line">  <span class="comment">// The socket object</span></div><div class="line">  <a name="_a0"></a><a class="code" href="classArSocket.html">ArSocket</a> sock;</div><div class="line"></div><div class="line">  <span class="comment">// Initialize Aria.  It is especially important to do</span></div><div class="line">  <span class="comment">// this on Windows, because it will initialize Window&#39;s</span></div><div class="line">  <span class="comment">// sockets system. </span></div><div class="line">  <a name="a1"></a><a class="code" href="classAria.html#ad99c16b5d947229d9f8e1c5b2d4cdd73">Aria::init</a>();</div><div class="line"></div><div class="line">  <span class="comment">// Connect to the server</span></div><div class="line">  <a name="a2"></a><a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a3"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketClientExample: Connecting to localhost TCP port 7777...&quot;</span>);</div><div class="line">  <span class="keywordflow">if</span> (sock.<a name="a4"></a><a class="code" href="classArSocket.html#ae9d803dcf9ad0bb72b790d3098ca8dc5">connect</a>(<span class="stringliteral">&quot;localhost&quot;</span>, 7777, ArSocket::TCP))</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketClientExample: Connected to server at localhost TCP port 7777.&quot;</span>);</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a name="a5"></a><a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;socketClientExample: Error connecting to server at localhost TCP port 7777: %s&quot;</span>, sock.<a name="a6"></a><a class="code" href="classArSocket.html#a940299aab2087e9247dece7b7fe01a5c">getErrorStr</a>().c_str());</div><div class="line">    <span class="keywordflow">return</span>(-1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Read data from the socket. read() will block until</span></div><div class="line">  <span class="comment">// data is received. </span></div><div class="line">  strSize=sock.<a name="a7"></a><a class="code" href="classArSocket.html#a6cf324be0075d3469a70d01d4740e23c">read</a>(buff, <span class="keyword">sizeof</span>(buff));</div><div class="line"></div><div class="line">  <span class="comment">// If the amount read is 0 or less, its an error condition.</span></div><div class="line">  <span class="keywordflow">if</span> (strSize &gt; 0)</div><div class="line">  {</div><div class="line">    buff[strSize]=<span class="charliteral">&#39;\0&#39;</span>; <span class="comment">// Terminate the string with a NULL character:</span></div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketClientExample: Server said: \&quot;%s\&quot;&quot;</span>, buff);</div><div class="line">  }</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;socketClientExample: Error in waiting/reading from the server.&quot;</span>);</div><div class="line">    <a name="a8"></a><a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(-1);</div><div class="line">    <span class="keywordflow">return</span>(-1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Send the string &#39;Hello Server&#39; to the server. write() should</span></div><div class="line">  <span class="comment">// return the same number of bytes that we told it to write. Otherwise,</span></div><div class="line">  <span class="comment">// its an error condition.</span></div><div class="line">  <span class="keywordflow">if</span> (sock.<a name="a9"></a><a class="code" href="classArSocket.html#af2cfbd564eec7020283f5a6fe41f558f">write</a>(strToSend, strlen(strToSend)) == strlen(strToSend))</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketClientExample: Said hello to the server.&quot;</span>);</div><div class="line">  <span class="keywordflow">else</span></div><div class="line">  {</div><div class="line">    <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a012daf6573594f91242f8dd7c02eb74b">ArLog::Terse</a>, <span class="stringliteral">&quot;socketClientExample: Error sending hello string to the server.&quot;</span>);</div><div class="line">    <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(-1);</div><div class="line">    <span class="keywordflow">return</span>(-1);</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="comment">// Now close the connection to the server</span></div><div class="line">  sock.<a name="a10"></a><a class="code" href="classArSocket.html#a89ab9a4e404526343ccf6ba3b6644c58">close</a>();</div><div class="line">  <a class="code" href="classArLog.html#a43a8b3789126c818f390f24bdbceccce">ArLog::log</a>(<a class="code" href="classArLog.html#ac8cc0fb3aa323ab2a1c21340fdd1dce3a7040faf60eeb155eaa85d439b1066ca1">ArLog::Normal</a>, <span class="stringliteral">&quot;socketClientExample: Socket to server closed.&quot;</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Uninitialize Aria and exit</span></div><div class="line">  <a class="code" href="classAria.html#a6abc3d39b3e9548063bb8e78375acc17">Aria::exit</a>(0);</div><div class="line">  <span class="keywordflow">return</span>(0);</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>
